home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / network / manageme / snmp-dev.000 / snmp-dev / examples / snmpwalk.c < prev   
Encoding:
C/C++ Source or Header  |  1995-07-19  |  10.6 KB  |  389 lines

  1. /*
  2.  * snmpwalk.c - send snmp GETNEXT requests to a network entity, walking a
  3.  * subtree.
  4.  *
  5.  */
  6. /**********************************************************************
  7.     Copyright 1988, 1989, 1991, 1992 by Carnegie Mellon University
  8.  
  9.                       All Rights Reserved
  10.  
  11. Permission to use, copy, modify, and distribute this software and its 
  12. documentation for any purpose and without fee is hereby granted, 
  13. provided that the above copyright notice appear in all copies and that
  14. both that copyright notice and this permission notice appear in 
  15. supporting documentation, and that the name of CMU not be
  16. used in advertising or publicity pertaining to distribution of the
  17. software without specific, written prior permission.  
  18.  
  19. CMU DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  20. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  21. CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  22. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  23. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  24. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  25. SOFTWARE.
  26. ******************************************************************/
  27. #include <sys/types.h>
  28. #include <netinet/in.h>
  29. #include <sys/time.h>
  30. #include <stdio.h>
  31. #include <netdb.h>
  32. #ifdef linux
  33. # include <stdlib.h>
  34. # include <string.h>
  35. # include <arpa/inet.h>
  36. #endif
  37.  
  38. #include <snmp/snmp.h>
  39. #include <snmp/mib.h>
  40. #include <snmp/asn1.h>
  41. #include <snmp/snmp_impl.h>
  42. #include <snmp/snmp_api.h>
  43. #include <snmp/snmp_client.h>
  44. #include <snmp/party.h>
  45. #include <snmp/context.h>
  46. #include <snmp/view.h>
  47. #include <snmp/acl.h>
  48.  
  49. oid objid_mib[] = {1, 3, 6, 1, 2, 1};
  50.  
  51. int    snmp_dump_packet = 0;
  52.  
  53. void
  54. usage(){
  55.     fprintf(stderr, "Usage: snmpwalk -v 1 hostname community [objectID]      or:\n");
  56.     fprintf(stderr, "Usage: snmpwalk [-v 2 ] hostname noAuth [objectID]      or:\n");
  57.     fprintf(stderr, "Usage: snmpwalk [-v 2 ] hostname srcParty dstParty context [objectID]\n");
  58. }
  59.  
  60. int
  61. main(argc, argv)
  62.     int        argc;
  63.     char    *argv[];
  64. {
  65.     struct snmp_session    session, *ss;
  66.     struct snmp_pdu *pdu, *response;
  67.     struct variable_list *vars;
  68.     int    arg;
  69.     char *hostname = NULL;
  70.     char *community = NULL;
  71.     int gotroot = 0, version = 2;
  72.     oid    name[MAX_NAME_LEN];
  73.     int name_length;
  74.     oid root[MAX_NAME_LEN];
  75.     int    rootlen, count;
  76.     int running;
  77.     int status;
  78.     int port_flag = 0;
  79.     int dest_port = 0;
  80.     oid src[MAX_NAME_LEN], dst[MAX_NAME_LEN], context[MAX_NAME_LEN];
  81.     int srclen = 0, dstlen = 0, contextlen = 0;
  82.     u_long srcclock = 0, dstclock = 0;            /* YYY: check init */
  83.     int clock_flag = 0;
  84.     struct partyEntry *pp;
  85.     struct contextEntry *cxp;
  86.     int trivialSNMPv2 = FALSE;
  87.     struct hostent *hp;
  88.     u_long destAddr;
  89.  
  90.     init_mib();
  91.     /*
  92.      * Usage: snmpwalk -v 1 hostname community [objectID]      or:
  93.      * Usage: snmpwalk [-v 2 ] hostname noAuth [objectID]      or:
  94.      * Usage: snmpwalk [-v 2 ] hostname srcParty dstParty context [objectID]
  95.      */
  96.     for(arg = 1; arg < argc; arg++){
  97.     if (argv[arg][0] == '-'){
  98.         switch(argv[arg][1]){
  99.         case 'd':
  100.             snmp_dump_packet++;
  101.             break;
  102.         case 'p':
  103.             port_flag++;
  104.             dest_port = atoi(argv[++arg]);
  105.             break;
  106.         case 'c':
  107.             clock_flag++;
  108.             srcclock = atoi(argv[++arg]);
  109.             dstclock = atoi(argv[++arg]);
  110.             break;
  111.         case 'v':
  112.             version = atoi(argv[++arg]);
  113.             if (version < 1 || version > 2){
  114.             fprintf(stderr, "Invalid version: %d\n", version);
  115.             usage();
  116.             exit(1);
  117.             }
  118.             break;
  119.         default:
  120.             printf("invalid option: -%c\n", argv[arg][1]);
  121.             break;
  122.         }
  123.         continue;
  124.     }
  125.     if (hostname == NULL){
  126.         hostname = argv[arg];
  127.     } else if (version == 1 && community == NULL){
  128.         community = argv[arg]; 
  129.     } else if (version == 2 && srclen == 0 && !trivialSNMPv2){
  130.         if (read_party_database(party_conf ()) > 0){
  131.         fprintf(stderr,
  132.             "Couldn't read party database from %s\n",
  133.                 party_conf ());
  134.         exit(0);
  135.         }
  136.         if (read_context_database(context_conf ()) > 0){
  137.         fprintf(stderr,
  138.             "Couldn't read context database from %s\n",
  139.             context_conf ());
  140.         exit(0);
  141.         }
  142.         if (read_acl_database(acl_conf ()) > 0){
  143.         fprintf(stderr,
  144.             "Couldn't read access control database from %s\n",
  145.                 acl_conf ());
  146.         exit(0);
  147.         }
  148.  
  149.         if (!strcasecmp(argv[arg], "noauth")){
  150.         trivialSNMPv2 = TRUE;
  151.         } else {
  152.         party_scanInit();
  153.         for(pp = party_scanNext(); pp; pp = party_scanNext()){
  154.             if (!strcasecmp(pp->partyName, argv[arg])){
  155.             srclen = pp->partyIdentityLen;
  156.             bcopy(pp->partyIdentity, src, srclen * sizeof(oid));
  157.             break;
  158.             }
  159.         }
  160.         if (!pp){
  161.             srclen = MAX_NAME_LEN;
  162.             if (!read_objid(argv[arg], src, &srclen)){
  163.             printf("Invalid source party: %s\n", argv[arg]);
  164.             srclen = 0;
  165.             usage();
  166.             exit(1);
  167.             }
  168.         }
  169.         }
  170.     } else if (version == 2 && dstlen == 0 && !trivialSNMPv2){
  171.         dstlen = MAX_NAME_LEN;
  172.         party_scanInit();
  173.         for(pp = party_scanNext(); pp; pp = party_scanNext()){
  174.         if (!strcasecmp(pp->partyName, argv[arg])){
  175.             dstlen = pp->partyIdentityLen;
  176.             bcopy(pp->partyIdentity, dst, dstlen * sizeof(oid));
  177.             break;
  178.         }
  179.         }
  180.         if (!pp){
  181.         if (!read_objid(argv[arg], dst, &dstlen)){
  182.             printf("Invalid destination party: %s\n", argv[arg]);
  183.             dstlen = 0;
  184.             usage();
  185.             exit(1);
  186.         }
  187.         }
  188.     } else if (version == 2 && contextlen == 0 && !trivialSNMPv2){
  189.         contextlen = MAX_NAME_LEN;
  190.         context_scanInit();
  191.         for(cxp = context_scanNext(); cxp; cxp = context_scanNext()){
  192.         if (!strcasecmp(cxp->contextName, argv[arg])){
  193.             contextlen = cxp->contextIdentityLen;
  194.             bcopy(cxp->contextIdentity, context,
  195.               contextlen * sizeof(oid));
  196.             break;
  197.         }
  198.         }
  199.         if (!cxp){
  200.         if (!read_objid(argv[arg], context, &contextlen)){
  201.             printf("Invalid context: %s\n", argv[arg]);
  202.             contextlen = 0;
  203.             usage();
  204.             exit(1);
  205.         }
  206.         }
  207.     } else {
  208.         rootlen = MAX_NAME_LEN;
  209.         if (read_objid(argv[arg], root, &rootlen)){
  210.         gotroot = 1;
  211.         } else {
  212.         printf("Invalid object identifier: %s\n", argv[arg]);
  213.         }
  214.     }
  215.     }
  216.  
  217.     if (gotroot == 0){
  218.     bcopy((char *)objid_mib, (char *)root, sizeof(objid_mib));
  219.     rootlen = sizeof(objid_mib) / sizeof(oid);
  220.     gotroot = 1;
  221.     }
  222.  
  223.     if (!hostname || (version < 1) || (version > 2)
  224.     || (version == 1 && !community)
  225.     || (version == 2 && (!srclen || !dstlen || !contextlen)
  226.         && !trivialSNMPv2)){
  227.             usage();
  228.             exit(1);
  229.     }
  230.  
  231.     if (trivialSNMPv2){
  232.     if ((destAddr = inet_addr(hostname)) == -1){
  233.         hp = gethostbyname(hostname);
  234.         if (hp == NULL){
  235.         fprintf(stderr, "unknown host: %s\n", hostname);
  236.         exit(1);
  237.         } else {
  238.         bcopy((char *)hp->h_addr, (char *)&destAddr,
  239.               hp->h_length);
  240.         }
  241.     }
  242.     srclen = dstlen = contextlen = MAX_NAME_LEN;
  243.     ms_party_init(destAddr, src, &srclen, dst, &dstlen,
  244.               context, &contextlen);
  245.     }
  246.  
  247.     if (clock_flag){
  248.     pp = party_getEntry(src, srclen);
  249.     if (pp){
  250.             pp->partyAuthClock = srcclock;
  251.             gettimeofday(&pp->tv, (struct timezone *)0);
  252.             pp->tv.tv_sec -= pp->partyAuthClock;
  253.     }
  254.     pp = party_getEntry(dst, dstlen);
  255.     if (pp){
  256.             pp->partyAuthClock = dstclock;
  257.             gettimeofday(&pp->tv, (struct timezone *)0);
  258.             pp->tv.tv_sec -= pp->partyAuthClock;
  259.     }
  260.     }
  261.  
  262.     bzero((char *)&session, sizeof(struct snmp_session));
  263.     session.peername = hostname;
  264.     if (port_flag)
  265.     session.remote_port = dest_port;
  266.     if (version == 1){
  267.     session.version = SNMP_VERSION_1;
  268.     session.community = (u_char *)community;
  269.     session.community_len = strlen((char *)community);
  270.     } else if (version == 2){
  271.     session.version = SNMP_VERSION_2;
  272.     session.srcParty = src;
  273.     session.srcPartyLen = srclen;
  274.     session.dstParty = dst;
  275.     session.dstPartyLen = dstlen;
  276.     session.context = context;
  277.     session.contextLen = contextlen;
  278.     }
  279.     session.retries = SNMP_DEFAULT_RETRIES;
  280.     session.timeout = 2000000L;
  281.     session.authenticator = NULL;
  282.     snmp_synch_setup(&session);
  283.     ss = snmp_open(&session);
  284.     if (ss == NULL){
  285.     printf("Couldn't open snmp\n");
  286.     exit(-1);
  287.     }
  288.  
  289.  
  290.     bcopy((char *)root, (char *)name, rootlen * sizeof(oid));
  291.     name_length = rootlen;
  292.  
  293.     running = 1;
  294.     while(running){
  295.     running = 0;
  296.     pdu = snmp_pdu_create(GETNEXT_REQ_MSG);
  297.  
  298.     snmp_add_null_var(pdu, name, name_length);
  299.  
  300.     status = snmp_synch_response(ss, pdu, &response);
  301.     if (status == STAT_SUCCESS){
  302.         if (response->errstat == SNMP_ERR_NOERROR){
  303.         for(vars = response->variables; vars;
  304.             vars = vars->next_variable){
  305.             if (vars->name_length < rootlen
  306.             || bcmp(root, vars->name, rootlen * sizeof(oid)))
  307.             continue;    /* not part of this subtree */
  308.             print_variable(vars->name, vars->name_length, vars);
  309.             if (vars->type != SNMP_ENDOFMIBVIEW
  310.             && vars->type != SNMP_NOSUCHOBJECT /* for robustness */
  311.             && vars->type != SNMP_NOSUCHINSTANCE){
  312.             bcopy((char *)vars->name, (char *)name,
  313.                   vars->name_length * sizeof(oid));
  314.             name_length = vars->name_length;
  315.             running = 1; /* restart so we can get next variable */
  316.             }
  317.         }
  318.         } else {
  319.         if (response->errstat == SNMP_ERR_NOSUCHNAME){
  320.             printf("End of MIB.\n");
  321.         } else {
  322.             printf("Error in packet.\nReason: %s\n",
  323.                snmp_errstring(response->errstat));
  324.             if (response->errstat == SNMP_ERR_NOSUCHNAME){
  325.             printf("The request for this object identifier failed: ");
  326.             for(count = 1, vars = response->variables; vars
  327.                 && count != response->errindex;
  328.                 vars = vars->next_variable, count++)
  329.                 /*EMPTY*/;
  330.             if (vars)
  331.                 print_objid(vars->name, vars->name_length);
  332.             printf("\n");
  333.             }
  334.         }
  335.         }
  336.  
  337.     } else if (status == STAT_TIMEOUT){
  338.         printf("No Response from %s\n", hostname);
  339.     } else {    /* status == STAT_ERROR */
  340.         printf("An error occurred, Quitting\n");
  341.     }
  342.  
  343.     if (response)
  344.         snmp_free_pdu(response);
  345.     }
  346.     snmp_close(ss);
  347.  
  348.     return 0;
  349. }
  350.  
  351.  
  352. #if 0
  353. /*
  354.  * to be part of security client library.
  355.  */
  356. find_params(srcParty, dstParty, context, ipaddress, entity, time, security)
  357.     struct partyEntry *srcParty, *dstParty;
  358.     struct contextEntry *context;
  359.     u_long ipaddress;
  360.     char *entity;
  361.     char *time;
  362.     char *security;
  363. {
  364.     struct partyEntry *pp, *goodParties[32];
  365.     struct contextEntry *cxp, *goodContexts[32];
  366.     struct aclEntry *ap;
  367.     int numParties = 0, numContexts = 0;
  368.  
  369.     party_scanInit();
  370.     for(pp = party_scanNext(); pp; pp = party_scanNext()){
  371.     if (pp->partyTDomain == 1 && !bcmp(pp->partyTAddress, &ipaddress, 4)){
  372.         if (security == 0 || *security == '\0' || !strcmp(security, "*")){
  373.         goodParties[numParties++] = pp;
  374.         } else if (!strcmp(security, "auth")
  375.                && (pp->partyAuthProtocol == 6)){
  376.         goodParties[numParties++] = pp;
  377.         } else if (!strcmp(security, "priv")
  378.                && (pp->partyPrivProtocol == 4)){
  379.         goodParties[numParties++] = pp;
  380.         }
  381.     }
  382.     }
  383.     /*
  384.      * Unfinished ...
  385.      */
  386. }
  387.  
  388. #endif
  389.